//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

/**
 * Persistent Memory Library Tests
 */

#include <config.h>
#include <stdio.h>
#include "check_common.h"
#include "pmalloc.h"

TEST(PmallocTest, TestPmalloc)
{
    int fd = open("pmalloc_test.dat", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    ASSERT_GE(fd, 0) << "open file fail";

    void *md = pmalloc_attach(fd, nullptr, 1024LL * 1024 * 1024 * 10, false);
    ASSERT_NE(md, nullptr) << "pmalloc_attach fail";

    void *key = nullptr;
    int i;

    for (i = 0;  i < 16;  i++) {
        key = pmalloc_getkey(md, i);
        if (nullptr != key) {
            // printf("Prekey... %p --> %s %lu\n", key, (char *)key,
            //        sizeof(size_t));
        }
        else {
            break;
        }
    }

    void *data = pmalloc(md, 0x1000 * 5);
    if (nullptr != data) {
        //sprintf((char *)data, "output data row %d", i);
        pmalloc_setkey(md, i, data);
    }

    size_t j;
    do {
        j = 0x1000 * random_number(1, 200);
        //printf("Allocating %zx... \n", j);
    } while (nullptr != pmalloc(md, j));

    pmalloc_detach(md);
    close(fd);
    //printf("Hello world... %p \n", data);
}

/*原项目在CTest下的测试用例代码*/

// int main() {
//     /*srand(time(nullptr));*/
//     int fd = open("pmalloc_test.dat", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
//     if (fd < 0)
//         return 1;
//     void *md = pmalloc_attach(fd, nullptr, 1024LL * 1024 * 1024 * 10);

//     void *key = nullptr;
//     int i;

//     /*key = pmalloc_getkey(md, 3);
//     pmfree(md, key);
//     pmalloc_setkey(md, 3, nullptr);*/

//     for (i = 0; i < 16; ++i) {
//         key = pmalloc_getkey(md, i);
//         if (nullptr != key)
//             printf("Prekey... %p --> %s %lu\n", key, (char *)key,
//                    sizeof(size_t));
//         else
//             break;
//     }

//     void *data = pmalloc(md, 0x1000 * 5);
//     if (nullptr != data) {
//         sprintf((char *)data, "output data row %d", i);
//         pmalloc_setkey(md, i, data);
//     }

//     size_t j;
//     do {
//         j = 0x1000 * random_number(1, 200);
//         printf("Allocating %zx... \n", j);
//     } while (nullptr != pmalloc(md, j));

//     pmalloc_detach(md);
//     close(fd);
//     printf("Hello world... %p \n", data);
//     return 0;
// }
